### Settings
CC			= gcc
CXX			= g++
CFLAGS		= -Os -pipe -Wno-unused-result
CXXFLAGS	= $(CFLAGS) -std=c++11
LDFLAGS		= -lpthread

TARGET		= supervisor
TEMDIR		= objs

### Auto-Generate
SRCS	= $(wildcard *.cc) $(wildcard *.c)
OBJS	= $(patsubst %.c, $(TEMDIR)/%.o, $(patsubst %.cc, $(TEMDIR)/%.o, $(SRCS)))
DIRS	= $(sort $(patsubst %/, %, $(dir $(OBJS))))

### Targets
library: prebuild build

prebuild:
	@echo -e "=== Start to build supervisor ==="
	@test -d '$(TEMDIR)' | mkdir -p $(TEMDIR)

build: $(OBJS)
	@echo -e ">>> Generate shared supervisor ..."
	@zip -r -9 -q res.zip res
	@objcopy -I binary -B i386 -O elf64-x86-64 res.zip res.o
	@$(CXX) -o $(TARGET) $(OBJS) res.o $(LDFLAGS)
	@rm -f res.zip res.o

clean:
	@rm -rf $(TEMDIR)

### Rules
$(TEMDIR)/%.o: %.cc
	@echo -e "--> Compile $< ..."
	@$(CXX) $(CXXFLAGS) -c $< -o $@

$(TEMDIR)/%.o: %.c
	@echo -e "--> Compile $< ..."
	@$(CC) $(CFLAGS) -c $< -o $@
